package com.rd.sys.common.fileexport.inter.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import baseproj.common.exception.BaseServiceException;

import com.rd.sys.common.base.CommonConstants;
import com.rd.sys.common.fileexport.inter.ExportOperator;
import com.rd.sys.common.utils.JacksonMapper;
import com.rd.sys.common.utils.client.AnalysisPathUtils;
import com.rd.sys.dto.analysis.statistic.StatisticExportDto;
import com.rd.sys.dto.client.data.GazeData;
import com.rd.sys.dto.commmon.base.BaseDto;

public class ResultRawDataExport extends ExportOperator {

    @Override
    public List<String> operation(BaseDto dto, String splitSign) throws BaseServiceException, Exception {
        StatisticExportDto exportDto = (StatisticExportDto) dto;

        if (exportDto.getPage().getCurrentPage() > 1) { // 由于是一次性从数据文件中获取导出数据,因此只用currentPage一次
            return null;
        }

        List<String> contentList = new ArrayList<String>();
        StringBuilder content = new StringBuilder();
        BufferedReader rawReader = null;
        GazeData gaze = null;
        try {
            File rawFile = new File(AnalysisPathUtils.findResultRawDataFileName(exportDto.getUserCode(),
                    exportDto.getProjectCode(), exportDto.getSchemeCode(), exportDto.getResultCode()));

            rawReader = new BufferedReader(new InputStreamReader(new FileInputStream(rawFile),
                    CommonConstants.SysParamConstants.SYS_CHARSET));

            String line = null;
            while ((line = rawReader.readLine()) != null) {
                gaze = (GazeData) JacksonMapper.json2Bean(line, GazeData.class);

                content.append(gaze.smoothedCoordinates.x).append(splitSign);
                content.append(gaze.smoothedCoordinates.y).append(splitSign);
                content.append(gaze.leftEye.smoothedCoordinates.x).append(splitSign);
                content.append(gaze.leftEye.smoothedCoordinates.y).append(splitSign);
//                content.append(gaze.leftEye.pupilCenterCoordinates.x).append(splitSign);
//                content.append(gaze.leftEye.pupilCenterCoordinates.y).append(splitSign);
                content.append(gaze.leftEye.pupilSize).append(splitSign);
                content.append(gaze.rightEye.smoothedCoordinates.x).append(splitSign);
                content.append(gaze.rightEye.smoothedCoordinates.y).append(splitSign);
//                content.append(gaze.rightEye.pupilCenterCoordinates.x).append(splitSign);
//                content.append(gaze.rightEye.pupilCenterCoordinates.y).append(splitSign);
                content.append(gaze.rightEye.pupilSize).append(splitSign);
                content.append(gaze.isFixated).append(splitSign);
                content.append(gaze.timeStamp);

                contentList.add(content.toString());
                content.delete(0, content.length());
                gaze = null;
            }

        } finally {
            if (rawReader != null) {
                rawReader.close();
                rawReader = null;
            }
        }

        return contentList;
    }
}
